'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入pvp地图
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var rivalService = require('../../../../services/rivalService');
var constantUtil = require('../../../../util/constantUtil');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['pvp', 'build', 'role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var pvp_model = player_manager.pvp;
                var build_model = player_manager.build;
                var role_model = player_manager.role;

                var player_list = pvp_model.player_list;

                var terrian_id_list = [];
                dataApi.pvp_config.each(function (value, key) {
                    if (!pvp_model.isRoadOpen(role_model.getLv(), key) || !!pvp_model.getRoadPlayer(key)) {
                        return;
                    }
                    if (value.terrain_id == 2) {
                        terrian_id_list.push(key);
                    }
                });

                var pvp_player_num = constantUtil.value('pvp_player_num') - pvp_model.player_list.length;
                if (pvp_player_num <= 0) {
                    return next(null, {
                        code: Code.OK
                    });
                }
                terrian_id_list = _.sampleSize(terrian_id_list, pvp_player_num);

                async.each(
                    terrian_id_list,
                    function (coordinate_id, callback) {
                        rivalService.getRandomRivalRoleId(pvp_model.role_id, pvp_model.getPlayerRoleIdList(), function (err, rival_role_id) {
                            if (!!rival_role_id) {
                                pvp_model.addRoadPlayer({
                                    road_id: coordinate_id,
                                    role_id: rival_role_id
                                });
                            }
                            return callback();
                        });
                    },
                    function (err) {
                        return cb();
                    }
                );
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
